<?php
/**
 * 首页相关接口
 */

namespace App\Http\Controllers\App\V3;

use App\Enums\ActionEnum;
use App\Response\Response;
use Illuminate\Http\Request;
use App\Exceptions\BasicException;
use App\Services\Tools\CurlService;
use App\Services\Tools\PushService;
use App\Http\Controllers\BaseController;
use App\Services\Tools\ConstellationService;

class IndexController extends BaseController
{
    public function home(Request $request)
    {
        $topNavData = app('repo_index')->getIndexNav();
        $recommendAnchorData = app('repo_recommend')->getRecommendAnchor(UID);
        $themeSquareData = app("repo_theme")->getThemeInfo();
        $hotRoomListData = app("repo_recommend")->getRecommendHotData(1, 10)->items();
        $isShowActivity = intval(!app("repo_user")->isUserHasLoginDay(UID, date("Y-m-d")));

        return Response::sendData([
            "top_nav"          => $topNavData,
            "recommend_anchor" => $recommendAnchorData,
            "theme_square"     => $themeSquareData,
            "hot_room_list"    => $hotRoomListData,
            "switch"           => [
                "is_show_activity" => $isShowActivity,
            ],
        ]);
    }

    public function refreshRecommend(Request $request)
    {
        $recommendAnchorData = app('repo_recommend')->getRecommendAnchor(UID);

        return Response::sendData($recommendAnchorData);
    }

    public function getHotList(Request $request)
    {
        // 参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'page'    => 'filled|numeric',
                'perPage' => 'filled|numeric',
            ]
        );

        if ($v->fails()) {
            throw new BasicException(10010, getValidateMessage($v->messages()));
        }

        $params = $request->input();

        $page = empty($params['page']) ? 1 : $params['page'];
        $perPage = empty($params['perPage']) ? 10 : $params['perPage'];

        $hotRoomListData = app("repo_recommend")->getRecommendHotData($page, $perPage);

        return Response::sendList($hotRoomListData);
    }

    public function planetHome(Request $request)
    {
        $topNavData = app('repo_index')->getIndexNav();
        $recommendAnchorData = app('repo_recommend')->getRecommendAnchor(UID, true);
        $themeSquareData = app("repo_theme")->getThemeInfo();
        $hotRoomListData = app("repo_recommend")->getRecommendHotData(1, 10)->items();
        $isShowActivity = intval(!app("repo_user")->isUserHasLoginDay(UID, date("Y-m-d")));
        $default_constellation = ConstellationService::getDefaultConsellation(UID);
        $constellation = ConstellationService::getData($default_constellation);

        return Response::sendData([
            "top_nav"          => $topNavData,
            "recommend_anchor" => $recommendAnchorData,
            "theme_square"     => $themeSquareData,
            "hot_room_list"    => $hotRoomListData,
            'constellation'    => $constellation,
            "switch"           => [
                "is_show_activity" => $isShowActivity,
            ],
        ]);
    }

    public function changeConstellation(Request $request)
    {
        $consName = $request->input('consName');
        if (!$consName) {
            new BasicException(0, '请选择星座');
        }
        if (!in_array($consName, ConstellationService::getConsList())) {
            new BasicException(0, '星座不存在');
        }
        $constellation = ConstellationService::getData($consName);
        ConstellationService::setDefaultConsellation(UID, $consName);

        return Response::sendData($constellation);
    }

    public function reportLog(Request $request)
    {
        $user_code = $request->input('user_code', '');
        $user_id = app('model_user_data')->where('user_code', $user_code)->value('user_id');
        if (!$user_id) {
            throw new BasicException(10014);
        }
        (new PushService($user_id))->reportLog([
            'user_id' => $user_id,
        ]);

        return Response::sendData(1);
    }
}
